<?php
/**
 * @name 直播系统管理控制器
 * @namespace app\admin\controller;
 * @use app\common\controller\AdminBase;
 * @date 2016-8-31
 * @author zhibo
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
class System extends AdminBase {
    /**
     * @name 初始化
     */
    protected function _initialize(){
        //添加初始化方法
        parent::_initialize();
    }
    /**
     * @name 清理缓存
     */
    public function clearCache(){
        system_log('清理了网站缓存','admin_system',['key'=>'cleancache']);
        //清文件缓存
        $dirs	=	array(APP_PATH.'/runtime/');
        
        //清理缓存
        foreach($dirs as $value) {
        	$this->rmdirr($value);
        	echo "<div style=';border:2px solid #0099CC; background:#f1f1f1; padding:20px;margin:20px;width:800px;font-weight:bold;color:#0099CC;text-align:center;margin: 0 auto;'>成功清除缓存！ </div> <br /><br />";
        }
        @mkdir($dirs[0],0777,true);
        exit;
    }
    /**
     * @name 删除文件夹
     */
    protected function rmdirr($dirname){
        if (!file_exists($dirname)) return false;
    	if (is_file($dirname) || is_link($dirname)) {
    		return unlink($dirname);
    	}
    	$dir = dir($dirname);
    	if($dir){
    		while (false !== $entry = $dir->read()) {
    			if ($entry == '.' || $entry == '..') {
    				continue;
    			}
    			$this->rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
    		}
    	}
    	$dir->close();
        rmdir($dirname);
    }
    /**
     * @name 获取直播用户列表
     */
    public function shielding(){
        $this->pageTitle = '敏感词';
        $model = model('admin/Config');
        $list = $model->getConfigList('filter_word');
        //print_r($list);exit;
        $this->assign('list',$list);
        $info = $model->getConfigList('filter_word_config');
        if(!empty($info['data'])){
                $this->assign('info',$info['data'][0]);
        }else{
            $data = [
                'value' => '*',
                'type'  => 'filter_word_config',
                'key'   => 'filter_word_config',
                'description' => '敏感词替换配置'
            ];
            $config_id = $model->addConfig($data);
            $this->assign('info',['config_id'=>$config_id,'value'=>'*']);
        }
        return $this->fetch();
    }
    /**
     * @name 敏感词--添加
     */
    public function shielding_add(){
        if(!IS_POST){
            $this->pageTitle = '添加敏感词';
            return $this->fetch();
        }
        $res = false;
        $keyword_arr = array_unique(array_filter(explode(',',$this->keyword)));
        if(!empty($keyword_arr)){
            $model = model('admin/Config');
            $data = [];
            foreach($keyword_arr as $v){
                $v = string_filter($v);
                if(!$v) continue;
                if($model->hasValue($v,'filter_word')){
                    continue;
                }
                $item = [
                    'type' => 'filter_word',
                    'key'   => 'filter_word',
                    'value' => $v,
                    'update_time' => NOW_TIME
                ];
                $data[] = $item;
            }
            if(empty($data)){
                return ['status'=>0,'message'=>'添加失败,敏感词已存在'];
            }
            $res = $model->addAll($data);
        }
        if($res){
            //$this->update_shielding_version();
            system_log('添加敏感词','admin_config',['key'=>'shielding_add']);
            return ['status'=>1,'data'=>['info'=>'添加成功','jumpurl'=>U('admin/System/shielding')]];
        }
        return ['status'=>0,'message'=>'添加失败,请重新尝试'];
    }
    /**
     * @name 敏感词-- 编辑
     */
    public function shielding_edit(){
        if(true !== $this->checkData(['config_id'])){
            return IS_POST ? ['status'=>0,'message'=>$this->error_info] : '';
        }
        $this->config_id = decrypt_id($this->config_id);
        $model = model('admin/Config');
        if(!IS_POST){
            $info = $model->getConfigById($this->config_id);
            if($info){
                $this->pageTitle = '编辑敏感词';
                $this->assign('info',$info);
                return $this->fetch();
            }
        }else{
            if($model->hasValue($this->keyword,'filter_word')){
                return ['status' => 0,'message'=>'修改失败,敏感词已存在'];
            }
            $data = [
                'value' => $this->keyword,
                'config_id' => $this->config_id
            ];
            
            $res = $model->editConfig($data);
            if($res){
                system_log('修改敏感词','admin_config',['key'=>'shielding_update']);
                return ['status' => 1,'data'=> ['info'=>'修改成功','jumpurl'=>U('admin/System/shielding')]];
            }
            return ['status' => 0,'message'=>'修改失败,未做任何修改'];
        }
    }
    /**
     * @name  敏感词 -- 删除
     */
    public function shielding_delete(){
        if(IS_POST){
           if(true !== $this->checkData(['config_id'])){
                return ['status'=>0,'message'=>$this->error_info];
            }
            $this->config_id = decrypt_id($this->config_id);
            $res = model('admin/Config')->deleteConfig($this->config_id);
            if($res){
                //更新版本
                //$this->update_shielding_version();
                system_log('删除敏感词','admin_config',['key'=>'shielding_delete']);
                return ['status' => 1,'data'=> ['info'=>'删除成功','jumpurl'=>U('admin/System/shielding')]];
            }
        }
        return ['status' => 0,'message'=>'删除失败,词库未做任何操作'];
    }
    /**
     * @name 敏感词--更改替换
     */
    public function shielding_config(){
        if(true !== $this->checkData(['config_id'])){
            return IS_POST ? ['status'=>0,'message'=>$this->error_info] : '';
        }
        $this->config_id = decrypt_id($this->config_id);
        if(IS_POST){
           $data = [
                'value' => $this->keyword,
                'config_id' => $this->config_id
            ];
            $res = model('admin/Config')->editConfig($data);
            if($res){
                //$this->update_shielding_version();
                system_log('修改敏感词替换配置','admin_config',['key'=>'shielding_config_update']);
                return ['status' => 1,'data'=> ['info'=>'修改成功','jumpurl'=>U('admin/System/shielding')]];
            }
        }
        return ['status' => 0,'message'=>'修改失败,未做任何修改'];
    }
    /**
     * @name 发布敏感词
     */
    public function publish(){
        if($this->type && in_array($this->type,['filter_word'])){
            $model = model('admin/Config');
            if($model->zip_conf($this->type)){
                $model->update_version('filter_version');//敏感词配置版本号
                $model->update_version('config_version');//配置接口版本号
                system_log('发布/更新敏感词库:【敏感词数据包更新,API配置更新】','admin_config',['key'=>'shielding_version_update']);
                return ['status'=>1,'data'=>['info'=>'更新词库成功']];
            }
        }
        return ['status'=>0,'message'=>'更新词库失败'];
    }
    /**
     * @name 展示系统日志
     */
    public function log(){
        $this->pageTitle = '系统日志';
        $list = model('admin/Log')->getLogList();
        $this->assign('list',$list);
        return $this->fetch('log_list');
    }
    /**
     * @name 导出用户---测试方法
     */
    public function user_export(){
        $xlsName  = "User";
		$xlsCell  = array(
    		array('uid','用户名'),
    		array('uname','昵称'),
            array('sex','性别'),
    		array('ticket','授权票据'),
    		array('usid','用户标识'),
    		array('use_status','使用状态'),
    		array('create_time','创建时间'),
    		array('last_save_time','上次更新时间')
		);
		$xlsModel = M('user');
		$xlsData  = $xlsModel->select();
		foreach ($xlsData as $k => &$v)
		{
			$v['sex'] = ($v['sex']==1)?'男':'女';
            $v['create_time'] = date('Ymd H:i:s',$v['create_time']);
            $v['last_save_time'] = $v['last_save_time'] ? date('Ymd H:i:s',$v['last_save_time']) : '';
		}
        model('admin/Excel')->export($xlsName,$xlsCell,$xlsData);
    }
    /**
     * @name 导出日志
     */
    public function log_export(){
        if(decrypt_id((session('aid')))){
            $xlsName  = "系统日志";
    		$xlsCell  = [
                ['log_id','日志编号'],
                ['content','操作详述'],
                ['type','操作对象类别'],
                ['ctime','操作时间'],
                ['uname','操作人员']
            ];
    		$xlsData = model('admin/Log')->getLogList(true)['data'];
            model('admin/Excel')->export($xlsName,$xlsCell,$xlsData);
        }
        header('HTTP/1.1 401 Unauthorized');
        header('status: 401 Unauthorized');
        exit;
    }
}